package com.cxsj.project.system.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cxsj.framework.web.domain.JsonResult;
import com.cxsj.project.system.entity.Category;
import com.cxsj.project.system.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Dempsey
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/api/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    //get请求（"/api/category/list"）
    //返回：商品类别跟_id,数组对象
    @GetMapping("/list")
    public JsonResult list() {
        List<Category> list = categoryService.list();
        return JsonResult.result(list);
    }

    //put请求（"/api/category/type",kind）
    //传递参数：id: key
    //name: string
    //结果：修改相应类别
    @PutMapping("/type")
    public JsonResult update(Integer kind, String name) {
        categoryService.update(new UpdateWrapper<Category>().set("name", name).eq("_id", kind));
        return JsonResult.success();
    }

}

